﻿using UnityEngine;

namespace HEFramework
{
    public static class HeroViewSystem
    {
        [EntitySystem]
        public class HeroViewAwakeSystem : AwakeSystem<HeroView>
        {
            protected override void Awake(HeroView _self)
            {
                var pc = _self.GetSceneChild<PrefabComponent>();
                var dc = _self.GetSceneChild<DataTableComponent>();
                var hero = _self.GetParent<Hero>();
                if (pc == null || dc == null || hero == null)
                {
                    return;
                }

                var data = dc.LoadDataTable<DRAttribute>().GetData(hero.ID);
                pc.GetPrefabAsync(data.ABPath, Formula.CN(data.ABName, 0), _ =>
                {
                    if (_self.IsDisposed)
                    {
                        pc.Recycle(data.ABPath, Formula.CN(data.ABName, 0), _);
                    }
                    else
                    {
                        _self.Behaviour = _.GetComponent<TransformBehaviour>();
                    }
                }, false);
            }
        }


        [EntitySystem]
        public class HeroViewUpdateSystem : UpdateSystem<HeroView>
        {
            protected override void Update(HeroView _self)
            {
                _self.LookAtCamera();
                _self.Update_Pos();
            }
        }


        [EntitySystem]
        public class HeroViewDestroySystem : DestroySystem<HeroView>
        {
            protected override void Destroy(HeroView _self)
            {
                if (_self.Behaviour == null)
                {
                    return;
                }

                var pc = _self.GetSceneChild<PrefabComponent>();
                var dc = _self.GetSceneChild<DataTableComponent>();
                var hero = _self.GetParent<Hero>();
                if (pc == null || dc == null || hero == null)
                {
                    GameObject.Destroy(_self.Behaviour.gameObject);
                    _self.Behaviour = null;
                    return;
                }

                var data = dc.LoadDataTable<DRAttribute>().GetData(hero.ID);
                pc.Recycle(data.ABPath, Formula.CN(data.ABName, 0), _self.Behaviour.gameObject);
                _self.Behaviour = null;
            }
        }


        private static void Update_Pos(this HeroView _self)
        {
            if (_self.Behaviour == null)
            {
                return;
            }

            var tc = _self.GetParentChild<TransformComponent>();
            if (tc == null)
            {
                return;
            }

            _self.Behaviour.gameObject.transform.position = tc.P.ToVector();
            if (!_self.Behaviour.gameObject.activeSelf)
            {
                _self.Behaviour.gameObject.SetActive(true);
            }
        }

        private static void LookAtCamera(this HeroView _self)
        {
            if (_self.Behaviour == null)
            {
                return;
            }

            var gc = _self.GetSceneChild<GameCamera>();
            if (gc == null)
            {
                return;
            }

            gc.SetGameObjectRotateToCamera(_self.Behaviour.GetTransform("Transform_Body"));
        }
    }
}